package com.beiing.tianshuai.tianshuai.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;

/**
 * 数量调整视图，可以通过加减按钮和输入框调整数量
 *
 * @author Jinda Yang
 * @date 2018/5/16
 */
public class AmountView extends LinearLayout {

    /**
     * 当前数量
     */
    private int mAmount = 1;

    /**
     * 可设置的最大数量
     */
    private int mMaxAmount = 1;

    private TextView tvAmount;
    private ImageButton btnAdd;
    private ImageButton btnSub;

    /**
     * 用户修改数量时的监听事件
     */
    private OnAmountChangeListener mOnAmountChangeListener;

    public AmountView(Context context) {
        super(context);
    }

    public AmountView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.view_amount, this);
        tvAmount = findViewById(R.id.tv_amount);
        btnAdd = findViewById(R.id.btn_amount_add);
        btnSub = findViewById(R.id.btn_amount_sub);

        //设置增加按钮的点击事件
        btnAdd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnAmountChangeListener != null) {
                    if (mAmount < mMaxAmount) {
                        mAmount++;
                        tvAmount.setText(String.valueOf(mAmount));
                    }
                    mOnAmountChangeListener.onAmountChange(mAmount);
                }
            }
        });

        //设置减少按钮的点击事件
        btnSub.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnAmountChangeListener != null) {
                    if (mAmount > 1) {
                        mAmount--;
                        tvAmount.setText(String.valueOf(mAmount));
                    }
                    mOnAmountChangeListener.onAmountChange(mAmount);
                }
            }
        });

        TypedArray obtainStyledAttributes = getContext().obtainStyledAttributes(attrs, R.styleable.AmountView);
        int btnWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_btn_width, LayoutParams.WRAP_CONTENT);
        int tvWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_tv_width, 80);
        int tvTextSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_tv_text_size, 0);
        obtainStyledAttributes.recycle();

        LayoutParams btnParams = new LayoutParams(btnWidth, LayoutParams.MATCH_PARENT);
        btnSub.setLayoutParams(btnParams);
        btnAdd.setLayoutParams(btnParams);

        LayoutParams textParams = new LayoutParams(tvWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        tvAmount.setLayoutParams(textParams);
        if (tvTextSize != 0) {
            tvAmount.setTextSize(tvTextSize);
        }
        tvAmount.setText("0");
    }

    public int getAmount() {
        return mAmount;
    }

    public void setAmount(int amount) {
        mAmount = amount;
    }

    public int getMaxAmount() {
        return mMaxAmount;
    }

    /**
     * 设置数量修改器的最大值
     * 当最大值大于0的时候设置当前数量为1
     * 当最大值不大于0的时候设置当前数量为0
     *
     * @param maxAmount 最大值
     */
    public void setMaxAmount(int maxAmount) {
        mMaxAmount = maxAmount;
        if (mMaxAmount > 0) {
            mAmount = 1;
        } else {
            mAmount = 0;
        }
        if (mOnAmountChangeListener != null) {
            mOnAmountChangeListener.onAmountChange(mAmount);
        }
        tvAmount.setText(String.valueOf(mAmount));
    }

    /**
     * 设置数量变化的监听事件
     *
     * @param onAmountChangeListener 监听事件
     */
    public void setOnAmountChangeListener(OnAmountChangeListener onAmountChangeListener) {
        mOnAmountChangeListener = onAmountChangeListener;
    }

    public interface OnAmountChangeListener {

        /**
         * 数量发生变化时的回调接口
         *
         * @param amount 操作后的数量
         */
        void onAmountChange(int amount);
    }
}
